package model
{
	import flash.data.SQLStatement;
	
	import mx.formatters.DateFormatter;
	
	public class ProductSupplier
	{
		private var _pk:String;
		private var _product_pk:String;
		private var _s_columns:String = "";
		private var _s_values:String;
		private var _s_insert_statement:String;
		private var _s_delete_statement:String;
		private var _a_parameters:Array;
		public var df_now:DateFormatter = new DateFormatter();
		
		public function ProductSupplier()
		{
			df_now.formatString = "YYYYMMDDHHNNSS";
			_a_parameters = new Array();
		}

		/**
		 * setter for pk column
		*/ 
		public function set pk(s_pk:String):void { 
			_pk = s_pk;
			_a_parameters[":pk"] = s_pk;
		}
		
		/**
		 * getter for pk column
		*/
		public function get pk():String {
			return _pk;
		}
		/**
		 * setter for product_pk column
		*/ 
		public function set product_pk(s_product_pk:String):void { 
			_product_pk = s_product_pk;
			_a_parameters[":product_pk"] = s_product_pk;
		}
		
		/**
		 * getter for product_pk column
		*/
		public function get product_pk():String {
			return _product_pk;
		}
		
		
		/**
		 * sets s_columns
		*/
		private function func_add_column(str_value:String):void {
			if (_s_columns.length > 0) _s_columns += "," + str_value;
			else _s_columns = str_value;
		}
		
		/**
		 * getter for s_columns
		*/
		private function get s_columns():String {
			return _s_columns;
		}
		
		/**
		 * prepare strings for insert statements
		*/
		private function func_concatenate_insertion():void {
			_s_columns = _s_values = "";
			if (_pk != "") func_add_column("pk");
			
			if (_product_pk != "") func_add_column("product_pk");
			
			var pattern:RegExp = /,/gi;
			_s_values = _s_columns.replace(pattern, ",:");
			_s_values = ":" + _s_values;
		}
		
		/**
		 * getter for s_insert_statement column
		*/
		public function get s_insert_statement():String {
			func_concatenate_insertion();
			return "INSERT INTO product_suppliers(" + _s_columns + ") VALUES(" + _s_values + ")";
		}
		
		/**
		 * getter for s_insert_statement column
		*/
		public function get s_delete_statement():String {
			_a_parameters = new Array();
			_a_parameters[":pk"] = this._pk;
			_a_parameters[":product_pk"] = this._product_pk;
			return "DELETE FROM product_suppliers WHERE pk = :pk AND product_pk = :product_pk";
		}
		
		/**
		 * getter for s_parameters
		*/
		public function func_set_sqlstatement(sql:SQLStatement, s_statement:String):void {
			sql.text = s_statement;
			for (var col:* in _a_parameters) {
				sql.parameters[col] = _a_parameters[col];
			}
		}
	}
}